$('document').ready(function () {
    // 加载页面时触发的事件
    if(parseInt(getCookie('code'))!==202){
        window.location.href="home.html"
    }
    // email-input框事件
    $('#email-input').blur(function () {
        if ($('#email-input').val() == '') {
            $('#email-input').css({
                'border': '2px solid red',
            })
            $('#email-input').prop('placeholder', '账号不能为空!')
        }else {
            $('#email-input').css('border','2px solid green')
        }
    })
    $('#email-input').keyup(function () {
        if ($('#email-input').val() == '') {
            $('#email-input').prop('placeholder', '账号格式错误!')
            $('#email-input').css('border','2px solid red')
        }else {
            $('#email-input').css('border','2px solid green')
        }
    })

    // password-input框事件
    $('#password-input').blur(function () {
        if ($('#password-input').val() == '') {
            $('#password-input').css({
                'border': '2px solid red',
            })
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#checkpsw').css('cursor','auto')
        } else if ($('#password-input').val().length > 0) {
            $('#password-input').css('border','2px solid green')
            $('#checkpsw').css('cursor','pointer')
        }
    })
    $('#password-input').keyup(function () {
        if ($('#password-input').val().length > 0) {
            $('#checkpsw').css('cursor','pointer')
            $('#password-input').css('border','2px solid green')
        }else {
            $('#checkpsw').css('cursor','auto')
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#password-input').css('border','2px solid red')
        }

    })
     // 查看密码
     $('#checkpsw').click(function(){
        if ($('#password-input').attr('type')=='password') {
            $('#password-input').attr('type','text')
        }else if($('#password-input').attr('type')=='text'){
            $('#password-input').attr('type','password')
        }
    })

    // type-input框事件
    $('#type-input').blur(function () {
        if ($('#type-input').val() == '') {
            $('#type-input').css({
                'border': '2px solid red',
            })
            $('#type-input').prop('placeholder', '学校类别不能为空!')
        } else if ($('#type-input').val().length > 0) {
            $('#type-input').css('border','2px solid green')
        }
    })
    $('#type-input').keyup(function () {
        if ($('#type-input').val().length > 0) {
            $('#type-input').css('border','2px solid green')
        }else {
            $('#type-input').prop('placeholder', '学校类别不能为空!')
            $('#type-input').css('border','2px solid red')
        }

    })

    // name-input框事件
    $('#name-input').blur(function () {
        if ($('#name-input').val() == '') {
            $('#name-input').css('border','2px solid red')
            $('#name-input').prop('placeholder', '名称不能为空!')
        }else {
            $('#name-input').css('border','2px solid green')
        }
    })
    $('#name-input').keyup(function () {
        if (($('#name-input').val() !== '')) {
            $('#name-input').css('border','2px solid green')
        }else { 
            $('#name-input').css('border','2px solid red')
            $('#name-input').prop('placeholder', '名称不能为空!')
        }
    })
    
    // address-input框事件
    $('#address-input').blur(function () {
        if ($('#address-input').val() == '') {
            $('#address-input').css('border','2px solid red')
            $('#address-input').prop('placeholder', '学校地址不能为空!')
        }else {
            $('#address-input').css('border','2px solid green')
        }
    })
    $('#address-input').keyup(function () {
        if (($('#address-input').val() !== '')) {
            $('#address-input').css('border','2px solid green')
        }else { 
            $('#address-input').css('border','2px solid red')
            $('#address-input').prop('placeholder', '学校地址不能为空!')
        }
    })

    // phone-input框事件
    $('#phone-input').blur(function () {
        if ($('#phone-input').val() == '') {
            $('#phone-input').css('border','2px solid red')
            $('#phone-input').prop('placeholder', '联系电话不能为空!')
        }else {
            $('#phone-input').css('border','2px solid green')
        }
    })
    $('#phone-input').keyup(function () {
        if (($('#phone-input').val() !== '')) {
            $('#phone-input').css('border','2px solid green')
        }else { 
            $('#phone-input').css('border','2px solid red')
            $('#phone-input').prop('placeholder', '联系电话不能为空!')
        }
    })

    // -------------------------------------------------------------
    // 注册
    $('#register').click(function(){
        if(($('#name-input').val() !== '') && $('#password-input').val().length > 0 && emailzz.test($('#email-input').val()) && $('#type-input').val().length>0 && $('#address-input').val().length>0 && $('#phone-input').val().length>0){
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/adm/add-sch',
                data: {
                    "schid": $('#email-input').val(),
                    "schname": $('#name-input').val(),
                    "schtype": $('#type-input').val(),
                    "schaddress": $('#address-input').val(),
                    "schphone": $('#phone-input').val(),
                    "schpassword": $('#password-input').val()
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#name-input').val('')
                        $('#password-input').val('')
                        $('#address-input').val('')
                        $('#email-input').val('')
                        $('#phone-input').val('')
                        $('#type-input').val('')

                        $('#myModal-public h4').html('添加成功')
                        $('#myModal-public-body').html('成功添加学校管理员')
                        $('#myModal-public').modal({
                            keyboard: true,
                        })
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        }else if(($('#name-input').val() !== '') && $('#password-input').val().length < 9 && $('#password-input').val().length > 0 && $('#email-input').val().length>0 && $('#type-input').val().length>0){
            $('#myModal-public h4').html('添加成功')
            $('#myModal-public-body').html('成功添加学校：编号：'+$('#email-input').val()+'')
            $('#myModal-public').modal({
                keyboard:true,
            })
        }else if($('#email-input').val()==''){
            $('#email-input').prop('placeholder', '账号格式错误!')
            $('#email-input').css('border','2px solid red')
        }else if($('#password-input').val()==''){
            $('#password-input').prop('placeholder', '密码格式错误!')
            $('#password-input').css('border','2px solid red')
        }else if($('#type-input').val()==''){
            $('#type-input').prop('placeholder', '学校类别不能为空!')
            $('#type-input').css('border','2px solid red')
        }else if($('#name-input').val()==''){
            $('#name-input').prop('placeholder', '名称不能为空!')
            $('#name-input').css('border','2px solid red')
        }else if($('#address-input').val()==''){
            $('#address-input').prop('placeholder', '学校地址不能为空!')
            $('#address-input').css('border','2px solid red')
        }else if($('#phone-input').val()==''){
            $('#phone-input').prop('placeholder', '联系电话不能为空!')
            $('#phone-input').css('border','2px solid red')
        }
    })
    // --------------------------
    // 模态框
    $('#myModal .modal-footer button').eq(1).click(function(){
        window.location.href="login.html"
    })
})